package com.kd.user.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.github.pagehelper.PageInfo;
import com.kd.user.entity.User;
import com.kd.user.entity.UserCondition;
import com.kd.user.service.UserService;
import com.kd.user.util.MailDetail;
import com.kd.user.util.SendEmail;
import com.kd.user.util.UUIDUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import net.minidev.json.JSONArray;
import org.json.JSONObject;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@CrossOrigin(origins="*")
@RestController
@Api(value = "用户管理",tags = {"common"})
public class UserController {
    @Autowired
    private UserService userService;
    private static final Logger logger = LoggerFactory.getLogger(UserController.class);
    @GetMapping("/hello")
    public String hello(){

        return "hello world!";
    }
    @GetMapping("/helloPython")
    public String helloPython(){
        Map<String,List> map=new HashMap<>();
        List<Map> newslist= new ArrayList<>();
        Map<String,String> picUrl=new HashMap<String,String>();
        picUrl.put("picUrl","www.baidu.com");
        newslist.add(picUrl);
        newslist.add(picUrl);
        newslist.add(picUrl);
        map.put("newslist",newslist);
        System.out.print(map);
        String jsonString = JSON.toJSONString(map, SerializerFeature.DisableCircularReferenceDetect);
        System.out.print(jsonString);
        return jsonString;

    }

    @PostMapping("/send")
    public String send(String map){

        Map m=JSON.parseObject(map);
        MailDetail mailDetail = new MailDetail();
        mailDetail.setMyEmailAccount((String)m.get("myEmailAccount"));
        mailDetail.setMyEmailPassword((String)m.get("myEmailPassword"));
        mailDetail.setMyEmailSMTPHost((String)m.get("myEmailSMTPHost"));
        mailDetail.setReceiveMailAccount((String)m.get("receiveMailAccount"));
        mailDetail.setSmtpPort((String)m.get("smtpPort"));
        mailDetail.setSendPeopleName((String)m.get("sendPeopleName"));
        mailDetail.setReceivePeopleName((String)m.get("receivePeopleName"));
        mailDetail.setSubject((String)m.get("subject"));
        mailDetail.setContent((String)m.get("content"));
        String result;
        try {
            SendEmail.sendMail(mailDetail);
            result="邮件发送成功";
        } catch (Exception e) {
            result="邮件发送失败";

        }
        return result;
    }

    @ApiOperation(value = "获取用户信息", notes = "根据用户id获取用户信息")
    @ApiImplicitParam(name = "id", value = "用户id", required = true,
            dataType = "string", paramType = "path")
    @GetMapping("/find/{id}")
    public User find(@PathVariable String id){

        User user = userService.queryByKey(id);
        return user;
    }
    @GetMapping("/findAll")
    @ApiOperation(value = "获取所有用户信息", notes = "获取所有用户信息")
    public PageInfo<User> findAll(int pageNum,int pageSize){
        PageInfo<User> list= userService.queryAllUser(pageNum,pageSize);
        return list;
    }
   @PostMapping("/findByCondition")
    @ApiOperation(value = "根据条件获取用户信息", notes = "条件查询")
    public PageInfo<User> findByCondition(UserCondition condition,int pageNum,int pageSize){
        System.out.println(condition);
       PageInfo<User> list= userService.queryUserByCondition(condition,pageNum,pageSize);
        return list;
    }

    @GetMapping("/remove")
    @ApiOperation(value = "删除指定用户信息", notes = "根据主键id删除")
    public Boolean remove(String id){
        return userService.removeUser(id);
    }

    @PostMapping("/updateById")
    @ApiOperation(value = "更新用户信息", notes = "根据主键id更新用户信息")
    public Boolean updateById(User user){
        return userService.updateById(user);
    }

    @PostMapping("/addUser")
    @ApiOperation(value = "新增用户信息", notes = "新增一个用户")
    public Boolean addUser(User user){
        System.out.println(user);
        user.setId(UUIDUtil.getUUID());
        boolean b=userService.addUser(user);
        System.out.println(b);
        return b;
    }
}
